﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dormsystem
{
    public partial class AddStudent : Form
    {
        public AddStudent()
        {
            InitializeComponent();
        }

        private void AddStudent_Load(object sender, EventArgs e)
        {
            //在combox中显示宿舍信息
            string sql = "select * from roominfo";

            DataTable dtRoom = DB.GetDataTable(sql);

            //进行数据绑定
            cboRoom.DataSource = dtRoom;
            cboRoom.DisplayMember = "RoomNo";
            cboRoom.ValueMember = "RoomID";
        }

        private Boolean ValidData()
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("请输入姓名");
                return false;
            }

            if (cboSex.Text == "")
            {
                MessageBox.Show("请输入性别");
                return false;
            }

            if (cboRoom.Text == "")
            {
                MessageBox.Show("请输入宿舍号");
                return false;
            }

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ValidData())
            {
                string name = txtName.Text;
                string sex = cboSex.Text;

                int roomID = (int)cboRoom.SelectedValue;
                int age = Convert.ToInt32(txtAge.Text);
                string classname = txtClassName.Text;
                string sno = txtSNO.Text;
                string phone = txtPhone.Text;
                string address = txtAddress.Text;

                //插入sql语句
                string sql = string.Format("insert into StudentInfo(name,sex,RoomID,age,classname,sno,phone,address,sid)" +
                    "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',4)", name, sex, roomID, age, classname, sno, phone, address);

                int result = Convert.ToInt32(DB.ExcuteSQL(sql));
                if (result>0)
                {
                    MessageBox.Show("学生信息添加成功");
                    txtName.Text = "";
                }
            }
        }
    }
}
